package com.sc.nft.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.PrizeDrawActivityAwardLog;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.ActivityLogDTO;
import com.sc.nft.mapper.PrizeDrawActivityAwardLogMapper;
import com.sc.nft.sup.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 抽奖活动的奖品日志(PrizeDrawActivityAwardLog)表Dao层
 *
 * @author wwc
 * @since 2023-08-23 15:35:31
 */
@Slf4j
@Repository
public class PrizeDrawActivityAwardLogDao extends ServiceImpl<PrizeDrawActivityAwardLogMapper, PrizeDrawActivityAwardLog> {

    public IPage<PrizeDrawActivityAwardLog> activityLog(ActivityLogDTO activityLogDTO, UserInfo userInfo) {
        LambdaQueryWrapper<PrizeDrawActivityAwardLog> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(PrizeDrawActivityAwardLog::getUserUuid, userInfo.getUserUuid());
        lambdaQueryWrapper.eq(PrizeDrawActivityAwardLog::getPrizeDrawActivityId, activityLogDTO.getId());
        lambdaQueryWrapper.orderByDesc(PrizeDrawActivityAwardLog::getCreateTime);
        return page(activityLogDTO.toMp(), lambdaQueryWrapper);
    }

    public List<PrizeDrawActivityAwardLog> getThreeSort(Long id) {
        LambdaQueryWrapper<PrizeDrawActivityAwardLog> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(PrizeDrawActivityAwardLog::getPrizeDrawActivityId, id);
        lambdaQueryWrapper.le(PrizeDrawActivityAwardLog::getSort, 3);
        lambdaQueryWrapper.groupBy(PrizeDrawActivityAwardLog::getSort);
        lambdaQueryWrapper.orderByDesc(PrizeDrawActivityAwardLog::getCreateTime);
        lambdaQueryWrapper.last("limit 3");
        return list(lambdaQueryWrapper);
    }

    public List<PrizeDrawActivityAwardLog> newestTen(Long id, List<Long> ids) {
        LambdaQueryWrapper<PrizeDrawActivityAwardLog> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(PrizeDrawActivityAwardLog::getPrizeDrawActivityId, id);
        lambdaQueryWrapper.notIn(CollUtil.isNotEmpty(ids),PrizeDrawActivityAwardLog::getId, ids);
        lambdaQueryWrapper.orderByDesc(PrizeDrawActivityAwardLog::getCreateTime);
        lambdaQueryWrapper.last("limit 10");
        return list(lambdaQueryWrapper);
    }

}
